<template>
  <div class="favorites-page">
    <div class="page-header">
      <h2>我的收藏</h2>
      <div class="stats">
        共收藏 {{ userStore.favoriteMovies.length }} 部影片
        <button v-if="userStore.favoriteMovies.length > 0" @click="clearAllFavorites" class="clear-btn">
          清空收藏
        </button>
      </div>
    </div>

    <div v-if="userStore.favoriteMovies.length === 0" class="empty-state">
      <div class="empty-icon">❤️</div>
      <h3>暂无收藏影片</h3>
      <p>将喜欢的影片添加到收藏夹，方便随时观看</p>
      <router-link to="/" class="browse-btn">去浏览影片</router-link>
    </div>

    <div v-else class="movies-grid">
      <div 
        v-for="movie in userStore.favoriteMovies" 
        :key="movie.id" 
        class="movie-card"
        @click="$router.push(`/movie/${movie.movieId}`)"
      >
        <div class="movie-poster">
          <img :src="movie.movieCover" :alt="movie.title" @error="handleImageError">
          <button 
            @click.stop="removeFromFavorites(movie.movieId)"
            class="remove-fav-btn"
            title="取消收藏"
          >
            ❤️
          </button>
        </div>
        <div class="movie-info">
          <h3 class="movie-title">{{ movie.movieTitle }}</h3>
          <p class="movie-year">{{ movie.movieYear }}</p>
          <div class="movie-genres">
            <span v-for="genre in movie.genres?.slice(0, 2)" :key="genre" class="genre-tag">
              {{ genre }}
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useUserStore } from '../store/user';
import { useRouter } from 'vue-router';
import { onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';

const userStore = useUserStore();
const router = useRouter();

onMounted(async () => {
  if (userStore.isLoggedIn) {
    // await userStore.initUserData();
  }
});

const handleImageError = (event) => {
  event.target.src = 'https://via.placeholder.com/300x450/2d3748/4a5568?text=No+Image';
};

const removeFromFavorites = async (movieId) => {
  try {
    await ElMessageBox.confirm(
      '确定要从收藏中移除吗？',
      '确认操作',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    );
    await userStore.removeFromFavorites(movieId);
    ElMessage.success('移除收藏成功');
  } catch (error) {
    if (error !== 'cancel') {
      console.error('移除收藏失败:', error);
    }
  }
};

const clearAllFavorites = async () => {
  try {
    await ElMessageBox.confirm(
      '确定要清空所有收藏吗？此操作不可恢复。',
      '确认操作',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    );
    
    
    // 批量移除所有收藏
    const movieIds = userStore.favoriteMovies.map(movie => 
       movie.movieId
    );
    await userStore.removeBatchFromFavorites(movieIds)
    ElMessage.success('清空收藏成功');
  } catch (error) {
    if (error !== 'cancel') {

      console.error('清空收藏失败:', error);

    }
  }
};
</script>

<style scoped>
.favorites-page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  width: 100%;
  box-sizing: border-box;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.stats {
  display: flex;
  align-items: center;
  gap: 16px;
  color: var(--muted);
}

.clear-btn {
  background: #e53e3e;
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--muted);
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 20px;
}

.empty-state h3 {
  margin-bottom: 10px;
  color: var(--text);
}

.browse-btn {
  display: inline-block;
  margin-top: 20px;
  padding: 10px 20px;
  background: var(--primary);
  color: white;
  text-decoration: none;
  border-radius: 6px;
}

.movies-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 20px;
}

.movie-card {
  background: var(--card);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.movie-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0,0,0,0.3);
}

.movie-poster {
  position: relative;
  aspect-ratio: 2/3;
}

.movie-poster img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.remove-fav-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(0,0,0,0.7);
  border: none;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 16px;
}

.movie-info {
  padding: 12px;
}

.movie-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.movie-year {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 8px;
}

.movie-genres {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.genre-tag {
  font-size: 10px;
  padding: 2px 6px;
  background: #2d3748;
  border-radius: 12px;
  color: var(--muted);
}

@media (max-width: 768px) {
  .favorites-page {
    padding: 10px;
  }
  
  .page-header {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }
  
  .movies-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 15px;
  }
}
</style>